#coding=utf8
import sys, MangaBase, re, urllib2, urlparse
import cmrhelper
from cmrhelper import GbkToUtf8, UtfToGbk
ServerList = ['']*20
class Parser(MangaBase.CMangaBase):
    "zxmh.net parser"
    def Start(self):
        self.IniRe()
        try:
            self.chaptercontent = urllib2.urlopen(self.m_url).read()
        except:
            self.m_mangalog.writeLog('get page content fail','fatal')
            return
        # if m_manganame is None,then parse manganame
        if not self.m_manganame:
            self.ParseName()
        self.GetChapters()
        if self.m_chapterlist:
            for item in self.m_chapterlist:
                self.ParseChapter(item[0], item[1])
        self.End()

    def IniRe(self):
        self.namere = re.compile("<strong>(.*?)免费在线漫画列表</strong>")
        self.chapterre = re.compile(\
            r"<td><a class=clink href=(.*?) target=_blank><img src=/images/c.gif border=0 hspace=5>(.*?)</a></td>")
        self.attre = re.compile(".*?htm\?a=(\d+?)&c=(\d+?)&p=(\d+?)&s=(\d+?)")
        self.picre = re.compile("<PicUrl Value=\"(.*?)\">")
        
        # urly code...
        self.ServerList = {}
        self.ServerList[1] = "http://76.99770.com:8001/"
        self.ServerList[2] = "http://16.99770.com:8002/"
        self.ServerList[3] = "http://122.99770.com:8003/"
        self.ServerList[4] = "http://123.99770.com:8004/"
        self.ServerList[5] = "http://76.99770.com:9977/"
        self.ServerList[6] = "http://16.99770.com:8006/"
        self.ServerList[7] = "http://16.99770.com:8007/"
        self.ServerList[8] = "http://122.99770.com:8008/"
        self.ServerList[9] = "http://122.99770.com:8009/"
        self.ServerList[10] = "http://232.99770.com:9977/"
        self.otherServer = "http://other.99770.com"

    def ParseName(self):
        "get manga name if name is None"
        self.m_mangalog.writeLog('get manga name')
        if self.namere.findall(self.chaptercontent):
            self.m_manganame = self.namere.findall(self.chaptercontent)[0]
            self.m_mangalog.writeLog('get manga name success, manga name [%s]' %
                                     self.m_manganame)
    def ParseAttrs(self, url):
        if self.attre.findall(url):
            self.Attr_a = self.attre.findall(url)[0][0]
            self.Attr_c = self.attre.findall(url)[0][1]
            self.Attr_p = self.attre.findall(url)[0][2]
            self.Attr_s = self.attre.findall(url)[0][3]
        
    def ParseChapter(self, curl, title):
        "parse each chapter to generate url of pictures"
        try:
            self.ParseAttrs(curl)
            turl = urlparse.urljoin(self.m_url, "/cocoxml/" + self.Attr_a + "/" + self.Attr_c + ".xml")
            content = urllib2.urlopen(turl).read()
            if content and self.picre.findall(content):
                for item in self.picre.findall(content):
                    purl = urlparse.urljoin(self.ServerList[int(self.Attr_s)], item)
                    self.PushPic([purl, purl, title, self.m_manganame])
        except  Exception, e:
            self.m_mangalog.writeLog(str(e) + ' in ParseChapter', 'error')
    
    def GetChapters(self):
        "get manga chapters"
        try:
            if self.chapterre.findall(self.chaptercontent):
                for item in self.chapterre.findall(self.chaptercontent):
                    self.m_chapterlist.append([urlparse.urljoin(self.m_url, item[0]),\
                                                   item[1]])
                    self.m_chaptercnt += 1
            self.m_mangalog.writeLog('get chapters finish total: [%d] chapters'\
                                         % self.m_chaptercnt)
        except  Exception, e:
            self.m_mangalog.writeLog(str(e)+' in getchapter','error')
